
# Include the BZFlag ruby module

include Bzflag

# Helper methods

def get_team_color(team_id)

	if RogueTeam == team_id
		team = "rogue"
	elsif RedTeam == team_id
		team = "red"
	elsif GreenTeam == team_id
		team = "green"
	elsif BlueTeam == team_id
		team = "blue"
	elsif PurpleTeam == team_id
		team = "purple"
	elsif RabbitTeam == team_id
		team = "rabbit"
	elsif HunterTeam == team_id
		team = "hunter"
	elsif Observers == team_id
		team = "observer"
	elsif Administrators == team_id
		team = "admin"
	else 
		team = "unknown"
	end

	return team
end


def show_player_info(to, from)

	if bz_get_player_index_list.find { |i| i == from}

		player = Player.new(from)	

		bz_send_text_message(BZ_SERVER, to ,"callsign => #{player.callsign}")
		bz_send_text_message(BZ_SERVER, to ,"groups   => #{player.groups}")
		bz_send_text_message(BZ_SERVER, to ,"operator => #{player.op?.to_s}")
		bz_send_text_message(BZ_SERVER, to ,"verified => #{player.verified?.to_s}")
		bz_send_text_message(BZ_SERVER, to ,"team     => #{get_team_color(player.team)}")
		bz_send_text_message(BZ_SERVER, to ,"ip       => #{player.ip_address}")
		bz_send_text_message(BZ_SERVER, to ,"lag      => #{player.lag}")
	else
		bz_send_text_message(BZ_SERVER, to ,"No player with playerid #{from}")
	end

end


def get_player_id_from_callsign(callsign)

	bz_get_player_index_list.each do |player_id|

		p =  Player.new(player_id)

		if p.callsign == callsign
			return player_id
		end
	end

	return -1
end


# Begin Tutor class

class Tutor < Plugin

	def player_join(event_data)

		# create a player object
	
		player = Player.new(event_data.player_id)

		bz_send_text_message(BZ_SERVER, player.player_id ,"Welcome, #{player.callsign} !")
		bz_send_text_message(BZ_SERVER, player.player_id ,"You joined the #{get_team_color(player.team)} team with ip #{player.ip_address} !")

	end


	def player_part(event_data)

		player = Player.new(event_data.player_id)
		bz_send_text_message(BZ_SERVER, BZ_ALLUSERS ,"Greetings from #{player.callsign} !")
	end

	def unknown_slash_command(event_data)

		# the command itself
		cmd = event_data.message.downcase.split[0]

		# the arguments if any
		params = event_data.message.split[1..-1].join(" ")

		# check if the cmd is the /whois command and mark the event
		# as handled , if not do nothing.
	
		if cmd != "/whois"	
			return false
		else
			event_data.handled = true
		end

		# if the param size is 0 it well return your own whois info,
		# else the whois info of the callsign you entered will show if 
		# the callsign is present.
		
		if params.size == 0
			show_player_info(event_data.from, event_data.from)
		else
			id = get_player_id_from_callsign(params)
			if id != -1
				show_player_info(event_data.from, id)
			else
				bz_send_text_message(BZ_SERVER, event_data.from , "No such callsign !")
			end
		end	

		return true

	end


end

# create a new Tutor plugin object
tutor = Tutor.new

# register the plugin object
tutor.register


